import { apiRequest } from './api'
import { KanbanData, TaskStatus, KanbanColumn } from '../types'

// 看板API接口
export const kanbanAPI = {
  // 获取看板数据
  async getKanbanData(): Promise<{
    success: boolean
    data: KanbanData
  }> {
    return apiRequest<{
      success: boolean
      data: KanbanData
    }>('/kanban')
  },

  // 更新任务状态
  async updateTaskStatus(
    taskId: string, 
    status: TaskStatus, 
    position: number = 0
  ): Promise<{
    success: boolean
    message: string
    data: any
  }> {
    return apiRequest<{
      success: boolean
      message: string
      data: any
    }>(`/kanban/tasks/${taskId}/status`, {
      method: 'PATCH',
      body: JSON.stringify({ status, position })
    })
  },

  // 批量更新任务位置
  async batchUpdateTaskPositions(updates: Array<{
    taskId: string
    status: TaskStatus
    position: number
  }>): Promise<{
    success: boolean
    message: string
    data: any
  }> {
    return apiRequest<{
      success: boolean
      message: string
      data: any
    }>('/kanban/reorder', {
      method: 'PATCH',
      body: JSON.stringify({ updates })
    })
  },

  // 获取看板列配置
  async getKanbanColumns(): Promise<{
    success: boolean
    data: KanbanColumn[]
  }> {
    return apiRequest<{
      success: boolean
      data: KanbanColumn[]
    }>('/kanban/columns')
  },

  // 更新看板列配置
  async updateKanbanColumn(
    columnId: string,
    updates: {
      title?: string
      color?: string
      is_visible?: boolean
    }
  ): Promise<{
    success: boolean
    message: string
    data: KanbanColumn
  }> {
    return apiRequest<{
      success: boolean
      message: string
      data: KanbanColumn
    }>(`/kanban/columns/${columnId}`, {
      method: 'PATCH',
      body: JSON.stringify(updates)
    })
  },

  // 获取看板统计数据
  async getKanbanStats(): Promise<{
    success: boolean
    data: Record<TaskStatus, {
      total: number
      p0: number
      p1: number
      p2: number
      p3: number
    }>
  }> {
    return apiRequest<{
      success: boolean
      data: Record<TaskStatus, {
        total: number
        p0: number
        p1: number
        p2: number
        p3: number
      }>
    }>('/kanban/stats')
  }
}
